#stop tomcat
#  1.shutdown 
#  2.T='ps aux |grep {1}' if shutdown no stop ，PID={1}
#  3.kill -9 {2}
stoptomcat()
{
echo "stop "${2}
# 1.shutdown
sh ${2}/shutdown.sh
wait 
#res_shut=$?
#echo $res_shut
#PS: this is a question about the return code of shutdown.sh, which is 0 but not stop the tomcat.WHY? 

# 2.test whether is stop 
re_PID=`ps aux|grep ${2}|grep -v grep|awk '{split($0,b," ");print b[2]}'`
echo "PID = "$re_PID
if [ -z "${re_PID}" ];then
	echo "PID is null"
	return 0
else
	echo "PID is no null"
fi
# 3.kill 
kill -9 $re_PID
echo "kill result: "$?

# 4.test whether is stop
whetherok $2
return $?

}

# test whether stop
# <unu> whether $1 and $2 are in the os process
# <du>  return null{0} nonull{1}
whetherok()
{
# <unu>
echo "whetherok------------------------$1:" $1
PID=`ps aux|grep ${1}|grep -v grep|grep tomcat|awk '{split($0,b," ");print b[2]}'`
echo "whetherok------------------------PID = "${PID}
if [ "" == "${PID}" ];then
        #echo "---------------------------------------------------------\n :PID is null\n---------------------------------------------"
        return 0
else
        #echo "-------------------------------------------------------\n :PID is not null\n---------------------------------------------"
	return 1
fi
}

#test whether dir is del
#<unu> $1 is del all  
#<du> [next] how to ?
whetherdel()
{
res=`ls $1 |grep -Ec "${2}\$"`
echo "-------------del "${res}
if [ 0 -eq ${res} ];then
	return 0 #ok
else 
	return 1 
fi
}

#test whether is new
#<unu> how many files in $1 is $2(the day[now])
#<du>  Is the number ok?[normal use 6] add 9/5/13 change for ExamApply ,old is 15
whethernew()
{
#<unu>
res=`ls -all $1 | grep -Ec $2`
echo "----------------new res: "$res
#<du>
if [ "${res}" -gt "6" ];then
	return 0 #ok
else
	return 1
fi
}


#change context
#<unu> read list get file|old|new
#<du> change
changefile()
{ 
#<unu>
con_file=${1}-pe/file.list
sou_file=$1
for LINE in `cat $con_file`
do
        echo $LINE
        file=`echo ${LINE}|awk '{split($0,b,"###");print b[1]}'`
	file=${sou_file}/WEB-INF/${file}
	echo "file : "${file}
        old=`echo ${LINE}|awk -F'###' '{print $2}'`
        echo "old :"${old}
        new=`echo ${LINE}|awk -F'###' '{print $3}'`
        echo "new :"${new}
 
#<du> 
        sed -i "s/${old}/${new}/g" `grep $old -rl ${file}`
        echo $?
done
}


#use testpay test
#d=`date +%m-%d`
#d='04-27'
#whethernew "/home/work/server/testpay_tomcat7/webapps/testpay" $d
#stoptomcat testpay "/home/work/server/testpay_tomcat7/bin"
#whetherdel "/home/work/server/testpay_tomcat7/bin" "*.sh"
